package cn.pzhuweb.service.imp;

import cn.pzhuweb.dao.CarDAO;
import cn.pzhuweb.dao.imp.CarDAOImp;
import cn.pzhuweb.pojo.Car;
import cn.pzhuweb.pojo.Message;
import cn.pzhuweb.service.CarService;

import java.util.List;

/**
 * @author Yancy xiao
 * @create 2022-11-15 10:38
 */
public class CarServiceImp implements CarService {
    private CarDAO dao = new CarDAOImp();

    @Override
    public List<Car> showAll() {
        return dao.selectAll();
    }

    @Override
    public Message updateCarInfo(Car car) {
        if (car == null) {
            return new Message(false, "数据不合法");
        }
        if ("".equals(car.getCar_id()) || "".equals(car.getCar_name()) || "".equals(car.getCar_price()) || "".equals(car.getCar_type())) {
            return new Message(false, "信息不完整");
        }
        int i = dao.update(car);
        if (i == 0) {
            return new Message(false, "更新失败！");
        }
        return new Message(true, "更新成功！");
    }

    @Override
    public Message removeCarInfo(Integer id) {
        if (id == null) {
            return new Message(false, "数据不合法");
        }
        int i = dao.delete(id);
        if (i == 0) {
            return new Message(false, "删除失败！");
        }
        return new Message(true, "删除成功！");
    }

    @Override
    public Message addCarInfo(Car car) {
        if (car == null) {
            return new Message(false, "数据不合法");
        }
        if ("".equals(car.getCar_id()) || "".equals(car.getCar_name()) || "".equals(car.getCar_price()) || "".equals(car.getCar_type())) {
            return new Message(false, "数据不完整");
        }
        int i = dao.insert(car);
        if (i == 0) {
            return new Message(false, "添加失败！");
        }
        return new Message(true, "添加成功！");
    }

    @Override
    public List<Car> searchByKeywords(String keywords) {
        return dao.selectByKeyWords(keywords);
    }

    @Override
    public Message isUsed(Integer id) {
        if (id == null) {
            return new Message(false, "数据不合法！");
        }
        Car dbCar = dao.selectById(id);
        if (dbCar == null) {
            return new Message(true, "恭喜，id号可用！");
        }
        return new Message(false, "id号已被占用！");
    }
}